package com.mall.user.services;

import com.alibaba.fastjson.JSON;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.user.IVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class IVerifyServiceImpl implements IVerifyService {
    @Autowired
    private UserVerifyMapper userVerifyMapper;

    @Autowired
    private MemberMapper memberMapper;

    /**
     * 用户验证
     * 将user_verify表及member表is_verify字段全部修改为Y
     *
     * @param userVerifyRequest
     * @return UserVerifyResponse
     */
    @Override
    public UserVerifyResponse verify(UserVerifyRequest userVerifyRequest) {
        UserVerifyResponse response = null;
        try {
            response = new UserVerifyResponse();
            String username = userVerifyRequest.getUserName();
            String uuid = userVerifyRequest.getUuid();

            userVerifyRequest.requestCheck();
            volidUserNameRepeat(userVerifyRequest);

            //修改user_verify表is_verify状态
            Example userVerifyExample = new Example(UserVerify.class);
            Example.Criteria criteria = userVerifyExample.createCriteria();
            criteria.andEqualTo("username", username);
            criteria.andEqualTo("uuid", uuid);
            UserVerify userVerify = new UserVerify();
            userVerify.setIsVerify("Y");
            int effectRows = userVerifyMapper.updateByExampleSelective(userVerify, userVerifyExample);
            if (effectRows != 1) {
                response.setCode(SysRetCodeConstants.DB_EXCEPTION.getCode());
                response.setMsg(SysRetCodeConstants.DB_EXCEPTION.getMessage());
                return response;
            }

            //修改member表is_verify状态
            Example memberExample = new Example(Member.class);
            memberExample.createCriteria().andEqualTo("username", username);
            Member member = new Member();
            member.setIsVerified("Y");
            member.setUpdated(new Date());
            int updateRows = memberMapper.updateByExampleSelective(member, memberExample);
            if (updateRows != 1) {
                response.setCode(SysRetCodeConstants.DB_EXCEPTION.getCode());
                response.setMsg(SysRetCodeConstants.DB_EXCEPTION.getMessage());
                return response;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("com.mall.user.services.IVerifyServiceImpl.verify occur exception: " + e);
            response.setCode(SysRetCodeConstants.USER_INFOR_INVALID.getCode());
            response.setMsg(SysRetCodeConstants.USER_INFOR_INVALID.getMessage());
            return response;
        }


        //写入日志
        log.info("用户激活成功，激活参数request:{}", JSON.toJSONString(userVerifyRequest));

        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return response;
    }

    /**
     * 验证用户信息是否一致及是否已激活
     *
     * @param userVerifyRequest
     */
    private void volidUserNameRepeat(UserVerifyRequest userVerifyRequest) {
        Example example = new Example(UserVerify.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", userVerifyRequest.getUserName());
        criteria.andEqualTo("uuid", userVerifyRequest.getUuid());
        List<UserVerify> userVerifyList = userVerifyMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(userVerifyList)) {
            throw new ValidateException(SysRetCodeConstants.REQUEST_CHECK_FAILURE.getCode(),
                    SysRetCodeConstants.REQUEST_CHECK_FAILURE.getMessage());
        }
        UserVerify userVerify = userVerifyList.get(0);
        String isVerify = userVerify.getIsVerify();
        if (!"N".equals(isVerify)) {
            throw new ValidateException(SysRetCodeConstants.USER_VERFIED_ERROR.getCode(),
                    SysRetCodeConstants.USER_VERFIED_ERROR.getMessage());
        }
    }
}
